(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.countryGroup.updateController', updateController);
	updateController.$inject = ['$q', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
	                            '$filter', 'countryGroupService' ];
	function updateController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, $filter, countryGroupService) {
		var ctrl = this, countryGroupInfo = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("countryGroupUpdate");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.countryGroup = {};
		ctrl.countryGroup.countrys = ctrl.countryGroup.countrys || [];
		
		ctrl.init = function() {
			var params = {
					countryGrpCode : countryGroupInfo.countryGrpCode
				};
				ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.subCountry.tableConfigs.table, rowSelectionChanged);
				ctrl.innerGridOptions.data = 'vm.countryGroup.countrys';
				authService.update(params).then(function(response) {
					ctrl.countryGroup = response.data || countryGroupInfo;
					ctrl.countryGroup.countrys = ctrl.countryGroup.countrys || [];
					angular.forEach(ctrl.countryGroup.countrys, function (con, key){
						con.dataStatus = ctrl.countryGroup.dataStatus;
					});
				});
		};

		ctrl.updateSave = function() {
			if (ctrl.countryGroup.taskId) {
				return authService.updateContinueSave(ctrl.countryGroup).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.countryGroup).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			if (ctrl.countryGroup.taskId) {
				return authService.updateContinueSubmit(ctrl.countryGroup).then(submitSuccessful, saveFailed);
			} else {
				return authService.updateSubmit(ctrl.countryGroup).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.addSubCountry = function(row) {
			return $q(function(resolved, rejected){
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.country',
					'params':{ multiSelect : true, "filterCountryCodes" : ctrl.countryGroup.countrys},
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}
							if (!ctrl.countryGroup.countrys) {
								ctrl.countryGroup.countrys = [];
							}
							angular.forEach( selectedRows, function (selectedOne, key) {
								selectedOne.dataStatus = 'C';
								selectedOne.countryCode = $filter('encode')(selectedOne.countryCode);
								ctrl.countryGroup.countrys.push(selectedOne);
							})
						},
						'close' : function(){
							resolved();
						}
					}
				});
			});
		}
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedCountrys = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.countryGroup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.countrygroup.list',
					'state' : 'cda.countryGroup.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.countryGroup = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
